package com.xjh.utils.beanutils.internal.logger;

import com.xjh.utils.beanutils.config.BeanCopyConfig;

import java.lang.reflect.Method;

/**
 * 日志
 *
 * @company 湖南省安第斯信息科技有限公司, 版权所有
 * @author kevin
 * @date 2021/3/10
 */
public class Logger {

	public static int LogLevelAll   = 0;
	public static int LogLevelTrace = 0;
	public static int LogLevelDebug = 1;
	public static int LogLevelInfo  = 2;
	public static int LogLevelWarn  = 3;
	public static int LogLevelError = 4;
	public static int LogLevelFatal = 5;
	public static int LogLevelNone  = 6;
	
	public static Logger getLogger(Class<?> clazz) {
		try {
			Class<?> loggerClass = Logger.class.getClassLoader().loadClass("org.apache.log4j.Logger");
			Method method = loggerClass.getMethod("getLogger", Class.class);
			Object logger = method.invoke(null, clazz);
			return new Logger(logger);
		}catch (Exception e) {
		}
		return new Logger(null);
	}
	
	Object logger = null;
	
	public Logger( Object logger ) {
		this.logger = logger;
	}
	
	public void trace(Object message) {
		
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("trace", Object.class);
				method.invoke(logger, message);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelTrace )
				return;
		}
	}
	
	public void trace(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("trace", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelTrace )
				return;
			t.printStackTrace();
		}
	}

	public void debug(Object message) {
		
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("debug", Object.class);
				method.invoke(logger, message);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelDebug )
				return;
		}
	}

	public void debug(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("debug", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelDebug )
				return;
			t.printStackTrace();
		}
	}

	public void info(Object message) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("info", Object.class );
				method.invoke(logger, message );
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelInfo )
				return;
		}
	}

	public void info(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("info", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelInfo )
				return;
			t.printStackTrace();
		}
	}

	public void warn(Object message) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("warn", Object.class);
				method.invoke(logger, message );
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelWarn )
				return;
		}
	}

	public void warn(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("warn", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelWarn )
				return;
			t.printStackTrace();
		}
	}

	public void error(Object message) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("error", Object.class );
				method.invoke(logger, message );
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelError )
				return;
		}
	}

	public void error(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("error", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() < LogLevelError )
				return;
			t.printStackTrace();
		}
	}

	public void fatal(Object message) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("fatal", Object.class, Throwable.class);
				method.invoke(logger, message);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() > LogLevelDebug )
				return;
		}
	}

	public void fatal(Object message, Throwable t) {
		if( logger != null ) {
			try {
				Method method = logger.getClass().getMethod("fatal", Object.class, Throwable.class);
				method.invoke(logger, message, t);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if( BeanCopyConfig.instance().getLogLevel() > LogLevelDebug )
				return;
			t.printStackTrace();
		}
	}
}
